package dhm.com.dhmshop.view.mine;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.lljjcoder.citypickerview.widget.CityPicker;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.BaseActiity;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.Bean;
import dhm.com.dhmshop.entity.GetAddr;
import dhm.com.dhmshop.entity.GetCityName;
import dhm.com.dhmshop.utils.SpUtils;
import dhm.com.dhmshop.utils.StringUtils;

public class AddaddrActivity extends BaseActiity implements LoginContract.IView {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.myaddress)
    TextView myaddress;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.del)
    TextView del;
    @BindView(R.id.addrs)
    EditText addrs;
    @BindView(R.id.quan)
    ImageView quan;
    @BindView(R.id.def_no)
    ImageView defNo;
    @BindView(R.id.def_yes)
    ImageView defYes;

    private boolean isDef=false;

    private PressenterImpl pressenter;
    private String uid;
    private String aid;


    List<String> province = new ArrayList<>();
    List<List<String>> city = new ArrayList<>();
    List<List<List<String>>> area = new ArrayList<>();


    String provinceid="";
    String cityid="";
    String areaid="";


    private OptionsPickerView pvOptions;
    private GetCityName getCityName;


    @Override
    protected int getLayout() {
        return R.layout.activity_addaddr;
    }

    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        Intent intent = getIntent();
        getWindow().setStatusBarColor(Color.WHITE);
        aid = intent.getStringExtra("aid");
        uid = SpUtils.getString(this, "uid");

        if (aid.equals("no")) {
            del.setVisibility(View.GONE);
            title.setText("添加收货地址");
        }else {
            title.setText("编辑收货地址");
            del.setVisibility(View.VISIBLE);
            Map<String,String> map=new HashMap<>();
            map.put("token",Constant.TOKEN);
            map.put("uid",uid);
            map.put("aid", aid);
            pressenter.sendMessage(this,Constant.GetAddress,map, GetAddr.class);
        }

        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        pressenter.sendMessage(this,Constant.GetCityName, map,GetCityName.class);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (((Bean) data).getCode()==1){
                finish();
            }
        }else if (data instanceof GetAddr){
            GetAddr getAddr= (GetAddr) data;
            name.setText(getAddr.getData().getUser_name());
            phone.setText(getAddr.getData().getMobile());
            myaddress.setText(getAddr.getData().getProvince()+getAddr.getData().getCity()+getAddr.getData().getArea());
            addrs.setText(getAddr.getData().getAddress());
            if (getAddr.getData().getTag().equals("默认")){
                isDef=true;
                quan.setBackgroundResource(R.drawable.back_quans);
                defYes.setVisibility(View.VISIBLE);
                defNo.setVisibility(View.GONE);
            }else {
                isDef=false;
                quan.setBackgroundResource(R.drawable.back_quan);
                defNo.setVisibility(View.VISIBLE);
                defYes.setVisibility(View.GONE);
            }
        }else if (data instanceof GetCityName){
            getCityName = (GetCityName) data;
            for (int i=0;i<getCityName.getData().size();i++){
                province.add(getCityName.getData().get(i).getAreaName());
                List<String> m_city=new ArrayList<>();
                List<List<String>> m_area=new ArrayList<>();
                for (int j=0;j<getCityName.getData().get(i).getChild().size();j++){
                    m_city.add(getCityName.getData().get(i).getChild().get(j).getAreaName());
                    List<String> h_area=new ArrayList<>();
                    for (int h=0;h<getCityName.getData().get(i).getChild().get(j).getAchlid().size();h++){
                        h_area.add(getCityName.getData().get(i).getChild().get(j).getAchlid().get(h).getAreaName());
                    }
                    m_area.add(h_area);
                }
                city.add(m_city);
                area.add(m_area);
            }

        }
    }

    @Override
    public void fail(String error) {

    }


    @OnClick({R.id.back, R.id.about, R.id.quan,  R.id.del, R.id.sure})
    public void onViewClicked(View view) {
        Map<String,String> map;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.about:

                //选择地址
                pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        myaddress.setText(getCityName.getData().get(options1).getAreaName()+getCityName.getData().get(options2).getChild().get(options2).getAreaName()+getCityName.getData().get(options1).getChild().get(options2).getAchlid().get(options3).getAreaName());
                        provinceid=getCityName.getData().get(options1).getAreaId()+"";
                        cityid=getCityName.getData().get(options1).getChild().get(options2).getAreaId()+"";
                        areaid=getCityName.getData().get(options1).getChild().get(options2).getAchlid().get(options3).getAreaId()+"";
                    }
                }).build();

                pvOptions.setPicker(province,city,area);//三级选择器
                pvOptions.show();
                break;
            case R.id.del:
                map=new HashMap<>();
                map.put("token",Constant.TOKEN);
                map.put("aid",aid);
                map.put("uid",uid);
                pressenter.sendMessage(AddaddrActivity.this,Constant.DelAddress,map,Bean.class);
                break;
            case R.id.quan:
                if (isDef){
                    quan.setBackgroundResource(R.drawable.back_quan);
                    defNo.setVisibility(View.VISIBLE);
                    defYes.setVisibility(View.GONE);
                }else {
                    quan.setBackgroundResource(R.drawable.back_quans);
                    defYes.setVisibility(View.VISIBLE);
                    defNo.setVisibility(View.GONE);
                }
                isDef=!isDef;
                break;
            case R.id.sure:
                String names = name.getText().toString();
                String phones = phone.getText().toString();
                String addrss = addrs.getText().toString();
                String myaddresss = myaddress.getText().toString();
                if (areaid.isEmpty()||areaid.equals("")||cityid.isEmpty()||cityid.equals("")||provinceid.isEmpty()||provinceid.equals("")||names.isEmpty()||names.equals("")||phones.isEmpty()||phones.equals("")||addrss.isEmpty()||addrss.equals("")||myaddresss.isEmpty()||myaddresss.equals("")){
                    Toast.makeText(this, "请检查数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (StringUtils.isRegistMobileNO(phones)){
                    Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }

                map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                map.put("user_name",names);
                map.put("add_mobile",phones);
                map.put("province",provinceid);
                map.put("city",cityid);
                map.put("area",areaid);
                map.put("address",addrss);
                if (isDef){
                    map.put("tag","默认");
                }else {
                    map.put("tag","家");
                }

                if (!aid.equals("no")) {
                    map.put("aid",aid);
                    pressenter.sendMessage(AddaddrActivity.this,Constant.EditAddress,map, Bean.class);

                }else {
                    pressenter.sendMessage(AddaddrActivity.this,Constant.AddAddress,map, Bean.class);

                }

                break;
            default:
        }
    }



}
